<style type="text/css">
    body {
        font-family: "宋体";
        font-size: 12px;
    }

    tr {
        height: 35px
    }

    .blue_color {
        color: #0000ff;
    }

    .red_color {
        color: #ff0000;
    }

    .green_color {
        color: green;
    }

    .button_css {
        font-family: "宋体";
        font-size: 12px;
        white-space: nowrap;
        width: 75px;
        height: 32px;
        font-weight: bold;
        margin-left: 380px
    }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<?php
define('CHARSET', 'utf-8'); //网站编码
@header("Content-type: text/html; charset=" . CHARSET);

/*
 * 根据远程地址抓取页面 css && js 及养生图片信息
 * @Author zhouyang
 * @date 2012-09-21
 */

Class DownLoadPageCssJs
{

    public $http_url = ''; //抓取地址
    public $page_name = ''; //保存页面名称
    public $root_dir = '/'; //下载文件根目录
    public $js_dir = 'js/'; //下载js存储地址
    public $css_dir = 'css/'; //下载css存储地址
    public $img_dir = 'img/'; //下载css图片存储地址
    public $css_img_path = ''; //远程css内包含图片路径
    public $save_path = true; //下载文件保存 路径 true 对应远程文件目录 false 当前目录

    //定义一个构造方法
    public function __construct($http_url, $page_name, $root_dir = '/', $js_dir = 'js/', $css_dir = 'css/', $img_dir = 'img/', $css_img_path, $save_path = true)
    {
        $this->http_url     = $http_url;
        $this->page_name    = $page_name;
        $this->root_dir     = $root_dir;
        $this->js_dir       = $js_dir;
        $this->css_dir      = $css_dir;
        $this->img_dir      = $img_dir;
        $this->css_img_path = $css_img_path;
        $this->save_path    = $save_path;
    }

    /*
     * 根据地址抓取页面 js css 地址
     */
    public function getHttpUrlCssJsInfo()
    {

        //存储抓取css js 地址用
        $css_js_arr = array();

        //加载解析html结构插件
        include "simple_html_dom.php";

        $html = file_get_html($this->http_url);

        // Find all script
        $script_arr = array();
        foreach ($html->find('script') as $element) {
            $str = explode('?version',$element->src);
            $script_arr[] = $str[0].'?'.md5(date('YmdHis'));
        }

        // Find all link
        $link_arr = array();
        foreach ($html->find('link') as $element) {
            $str = explode('?version',$element->href);
            $link_arr[] = $str[0].'?'.md5(date('YmdHis'));
        }

        $css_js_arr = array(
            'link'   => $link_arr,
            'script' => $script_arr
        );

        $this->css_js_arr = $css_js_arr;

        return $css_js_arr;
    }

    /*
     * 下载当前页面文件信息
     */
    public function savePage()
    {
        //检查当前要保存的页面是否存在
        echo '************************************************************************<br><br>';
        $check_file = $this->checkFileExists($this->root_dir . '/' . $this->page_name);

        if ($check_file) {
            echo "<font class='blue_color'>The file $this->root_dir/$this->page_name exists</font><br>";
        } else {
            $ret = $this->createDir($this->root_dir);

            if ($ret) {
                echo "Create The file $this->root_dir success<br>";

                $copy_ret = copy($this->http_url, $this->root_dir . '/' . $this->page_name);

                if ($copy_ret) {
                    echo "Copy The file $this->http_url to $this->root_dir/$this->page_name success<br>";
                } else {
                    echo "<font class='red_color'>Copy The file $this->http_url faile</font><br>";
                }
            } else {
                echo "<font class='red_color'>Create The file $this->root_dir faile</font><br>";
            }
        }
    }

    /*
     * 下载css文件
     */
    public function downLoadCss()
    {
        // Find all link
        echo '<br>************************************************************************<br><br>';
        $link_null_num = 0;
        foreach ($this->css_js_arr['link'] as $k => $v) {
            if ($v && $v != '/favicon.ico') {
                $ret = copy($v, $this->root_dir . '/' . $this->css_dir . $this->retrieve($v));

                if ($ret) {
                    echo "Copy The file $v success<br>";
                } else {
                    echo "<font class='red_color'>Copy The file $v faile.</font><br>";
                }
            } else {
                echo "<font class='green_color'>Do not Copy The file $v.</font><br>";
                $link_null_num++;
            }
        }
    }

    /*
     * 下载Js文件
     */
    public function downLoadJs()
    {
        // Find all script
        echo '<br>************************************************************************<br><br>';
        $script_null_num = 0;
        foreach ($this->css_js_arr['script'] as $k => $v) {
            if ($v) {
                $ret = copy($v, $this->root_dir . '/' . $this->js_dir . $this->retrieve($v));

                if ($ret) {
                    echo "Copy The file $v success<br>";
                } else {
                    echo "<font class='red_color'>Copy The file $v faile.</font><br>";
                }
            } else {
                echo "<font class='green_color'>Do not Copy The file script tabs.</font><br>";
                $script_null_num++;
            }
        }
    }

    /*
     * 下载css_img文件
     */
    public function downLoadCssImg()
    {
        echo '<br>************************************************************************<br><br>';
        foreach ($this->css_js_arr['link'] as $k => $v) {
            if ($v && $v != '/favicon.ico') {
                $data = file_get_contents($v);
                preg_match('/(.*\/\/.*?)\//', $this->css_img_path, $host);
                $host = $host[1];

                $regex = '/url\(\'{0,1}\"{0,1}(.*?)\'{0,1}\"{0,1}\)/';
                preg_match_all($regex, $data, $result);

                $result = array_unique($result[1]);

                $true_arr  = array();
                $false_arr = array();

                foreach ($result as $val) {
                    if (preg_match('/^http.*/', $val)) {
                        $target = $val;
                    } else if (preg_match('/^\/.*/', $val)) {
                        $target = str_replace('../', '', $host . $val);
                    } else {
                        $target = str_replace('../', '', $this->css_img_path . $val);
                    }

                    preg_match('/.*\/(.*\.\D+)$/', $val, $name);

                    $dir1 = str_replace($this->css_img_path, '', $target);
                    $dir2 = explode('/', $dir1);
                    array_pop($dir2);
                    $dir = implode('/', $dir2) . '/';

                    if (@fopen($target, 'r')) {
                        $true_arr[] = $target;

                        echo $this->root_dir . '/' . dirname($dir1);
                        $this->createDir($this->root_dir . '/' . dirname($dir1));

                        if (!is_file($this->root_dir . $dir . $name[1])) {
                            $ret = copy($target, $this->root_dir . '/' . $dir . $name[1]);

                            if ($ret) {
                                echo "Copy The file $target success<br>";
                            } else {
                                echo "<font class='red_color'>Copy The file $target faile.</font><br>";
                            }
                        } else {
                            echo "<font class='red_color'>Copy The file $target name is error.</font><br>";
                        }
                    } else {
                        $false_arr[] = $target;
                        echo "<font class='red_color'>Open The file $target faile.</font><br>";
                    }
                }
                //echo '<br>成功下载图片：<br><br>';
                //print_r(array_unique($true_arr));

                //echo '<br>下载失败图片：<br><br>';
                //print_r(array_unique($false_arr));
            }
        }
    }

    /*
    * 创建文件夹
    */
    public function createDir($dir, $mode = 0777)
    {
        $ret = is_dir($dir) or ($this->createDir(dirname($dir)) and mkdir($dir, $mode));

        if ($ret) {
            return TRUE;
        }
        return FALSE;
    }

    /*
     * 检查文件是否已经抓取
     * @parame $file_path string 文件本地存储地址
     */
    public function checkFileExists($file_path)
    {
        if (file_exists($file_path)) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 获取http地址文件名
     */
    public function retrieve($url)
    {
        preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/', $url, $match);
        return $match[1];
    }
}


/****************************************************************************************************/
$http_url     = isset($_POST['http_url']) ? $_POST['http_url'] : ''; //抓取地址
$root_dir     = isset($_POST['root_dir']) ? $_POST['root_dir'] : ''; //下载文件根目录
$js_dir       = isset($_POST['js_dir']) ? $_POST['js_dir'] : ''; //下载js存储地址
$css_dir      = isset($_POST['css_dir']) ? $_POST['css_dir'] : ''; //下载css存储地址
$img_dir      = isset($_POST['img_dir']) ? $_POST['img_dir'] : ''; //下载css图片存储地址
$css_img_path = isset($_POST['css_img_path']) ? $_POST['css_img_path'] : ''; //远程css内包含图片路径
$save_path    = true; //下载文件保存 路径 true 对应远程文件目录 false 当前目录

$type = isset($_POST['type']) ? $_POST['type'] : '';

if (@$_POST['submit']) {
    if ($http_url && $css_img_path) {

        switch($type){
            case 'wanzijia':
                $page_arr[] =  array( 'name' => 'index', 'content' => '' );
                $page_arr[] =  array( 'name' => 'index', 'content' => '&content=no' );
                $page_arr[] =  array( 'name' => 'chuanmen', 'content' => '' );
                $page_arr[] =  array( 'name' => 'recommend', 'content' => '' );
                $page_arr[] =  array( 'name' => 'list', 'content' => '' );
                $page_arr[] =  array( 'name' => 'search', 'content' => '' );
                $page_arr[] =  array( 'name' => 'house_index', 'content' => '' );
                $page_arr[] =  array( 'name' => 'house_index2', 'content' => '' );
                $page_arr[] =  array( 'name' => 'house_my', 'content' => '' );
                $page_arr[] =  array( 'name' => 'show', 'content' => '' );
                $page_arr[] =  array( 'name' => 'invite', 'content' => '' );
                $page_arr[] =  array( 'name' => 'setting_info', 'content' => '' );
                $page_arr[] =  array( 'name' => 'setting_avatar', 'content' => '' );
                $page_arr[] =  array( 'name' => 'setting_passwd', 'content' => '' );
                $page_arr[] =  array( 'name' => 'setting_mail', 'content' => '' );
                $page_arr[] =  array( 'name' => 'setting_synchro', 'content' => '' );
                $page_arr[] =  array( 'name' => 'setting_address', 'content' => '' );
                $page_arr[] =  array( 'name' => 'page_prompt', 'content' => '' );
                $page_arr[] =  array( 'name' => 'dialog', 'content' => '' );
                break;
            case 'ipd':
                $page_arr[] =  array( 'name' => 'index', 'content' => '' );
                $page_arr[] =  array( 'name' => 'channel', 'content' => '' );
                $page_arr[] =  array( 'name' => 'channel', 'content' => '&debug=1&skin=1' );
                $page_arr[] =  array( 'name' => 'channel', 'content' => '&act=2&debug=1&skin=1&bg=1' );
                $page_arr[] =  array( 'name' => 'play', 'content' => '' );
                $page_arr[] =  array( 'name' => 'play2', 'content' => '' );
                $page_arr[] =  array( 'name' => 'single', 'content' => '' );
                $page_arr[] =  array( 'name' => 'my_channel', 'content' => '' );
                $page_arr[] =  array( 'name' => 'my_rss', 'content' => '' );
                $page_arr[] =  array( 'name' => 'my_rss2', 'content' => '' );
                $page_arr[] =  array( 'name' => 'upload_page', 'content' => '' );
                $page_arr[] =  array( 'name' => 'upload_page', 'content' => '&step=2' );
                $page_arr[] =  array( 'name' => 'upload_page', 'content' => '&step=3' );
                $page_arr[] =  array( 'name' => 'upload', 'content' => '' );
                $page_arr[] =  array( 'name' => 'upload', 'content' => '&step=2' );
                $page_arr[] =  array( 'name' => 'upload', 'content' => '&step=3' );
                $page_arr[] =  array( 'name' => 'anhui', 'content' => '' );
                $page_arr[] =  array( 'name' => 'anhui_list', 'content' => '' );
                $page_arr[] =  array( 'name' => 'dialog', 'content' => '' );
                break;
        }
        /*
         * 使用示例
         */
        foreach($page_arr as $k => $v){
            $DownLoadPageCssJs = new DownLoadPageCssJs($http_url.'name='.$v['name'].$v['content'], $v['name'].$v['content'].'.php', $root_dir, $js_dir, $css_dir, $img_dir, $css_img_path, $save_path);

            $DownLoadPageCssJs->getHttpUrlCssJsInfo();

            $DownLoadPageCssJs->savePage();

            $DownLoadPageCssJs->downLoadCss();

            $DownLoadPageCssJs->downLoadJs();

            $DownLoadPageCssJs->downLoadCssImg();

            echo '<br>Download Success!<a href="copy_model.php" >'.$v["name"].$v['content'].'返回</a> <br>';
        }


    } else {
        echo '<br>Parameter is not enough!<a href="copy_model.php" >返回</a> <br>';
    }

    exit;
}


?>

<form action="copy_model.php" method="post">
    <table width="900px">
        <tr>
            <td colspan="3">
                <h1>网页信息抓取页面</h1>
            </td>
        </tr>
        <tr>
            <td width="140px">
                抓取地址：
            </td>
            <td>
                <input type="text" name="http_url" style="width: 450px">
            </td>
            <td width="240px">
                <span id="_http_url" style="color: red">地址不能为空</span>
            </td>
        </tr>

        <tr>
            <td>
                根目录：
            </td>
            <td>
                <input type="text" name="root_dir" style="width: 450px" value="ipd">
            </td>
            <td>
                <span id="_root_dir" style="color: red">根目录不能为空</span>
            </td>
        </tr>
        <tr>
            <td>
                JS目录：
            </td>
            <td>
                <input type="text" name="js_dir" style="width: 450px" value="js/">
            </td>
            <td>
                <span id="_js_dir" style="color: red">JS目录不能为空</span>
            </td>
        </tr>
        <tr>
            <td>
                CSS目录：
            </td>
            <td>
                <input type="text" name="css_dir" style="width: 450px" value="css/">
            </td>
            <td>
                <span id="_css_dir" style="color: red">CSS目录不能为空</span>
            </td>
        </tr>
        <tr>
            <td>
                图片目录：
            </td>
            <td>
                <input type="text" name="img_dir" style="width: 450px">
            </td>
            <td>
                <span id="_img_dir" style="color: red">图片目录不能为空</span>
            </td>
        </tr>
        <tr>
            <td>
                远程图片地址：
            </td>
            <td>
                <input type="text" name="css_img_path" style="width: 450px" value="http://10.1.3.27/PPSProject/ipd/trunk/">
            </td>
            <td>
                <span id="_css_img_path" style="color: red">远程图片地址不能为空</span>
            </td>
        </tr>
        <tr>
            <td>
                抓取类型：
            </td>
            <td>
                <input type="text" name="type" style="width: 450px" value="ipd">
            </td>
            <td>
                <span id="_type" style="color: red">抓取类型不能为空</span>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <input id="_down_submit" class="button_css" type="submit" name="submit" value=" 抓  取 ">
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
</form>
<script type="text/javascript">
    $(function () {
        $("span").hide();
        $("#_down_submit").bind('click', function () {
            var http_url = $("input[name=http_url]").val();
            if (!http_url) {
                $("span").hide();
                $("#_http_url").show();
                return false;
            }

            var root_dir = $("input[name=root_dir]").val();
            if (!root_dir) {
                $("span").hide();
                $("#_root_dir").show();
                return false;
            }

            var css_img_path = $("input[name=css_img_path]").val();
            if (!css_img_path) {
                $("span").hide();
                $("#_css_img_path").show();
                return false;
            }
        });
    });
</script>

